from util import run_command, run_command_stdout
import tempfile, os


class DMTX(object):

  def __init__(self):
    self.__read_command = 'dmtxread'
    self.__write_command = 'dmtxwrite'

  def set_read_command(self, read_command):
    self.__read_command = read_command

  def set_write_command(self, write_command):
    self.__write_command = write_command

  def generate(self, text, output):
    # command: dmtxwrite message.txt -o output
    tmptext = tempfile.NamedTemporaryFile(delete=False)
    tmptext.write(text)
    tmptext.close()
    run_command(self.__write_command, [ tmptext.name, '-o', output ])
    os.unlink(tmptext.name)

  def parse(self, input):
    # command: dmtxread -N1 input.png 
    text = run_command_stdout(self.__read_command, [ '-n', '-N1', input ])
    return text
    
